//package org.activoricordi.app.snippet
package edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.snippet

/**
 * Created with IntelliJ IDEA.
 * User: naru
 * Date: 3/30/13
 * Time: 11:41 AM
 * To change this template use File | Settings | File Templates.
 */
import net.liftweb.common.{Box,Full,Empty,Failure,ParamFailure}
import net.liftweb._
import  edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.model._
import util._
import util.Helpers._
import http._
import SHtml._
import S._
import mapper._
import util._
import xml.{Text, NodeSeq}


class DeleteCompound {

  /**
   * get the id of current Compound
   */
  var id = S.param("id") openOr ""
  /**
   * find the Compound by id
   */
  var compound = try {
    Compound.findByKey(id.toLong)
  } catch {
    case e:NumberFormatException => Empty
  }

  def delete (html: NodeSeq): NodeSeq ={

    compound map ({ i =>

    /**
     * delete Compound function
     */
      def deleteItem() :Unit = {
        i.delete_!
        S.redirectTo("/compound/list")
      }

      /**
       * show compound for verify Delete
       */
      bind("compound", html,
        "name" -> i.compoundName,
        "retentionTime" -> i.retentionTime,
        "inchiKey" -> i.inchiKey,
        "comments" -> i.comments,
        "submit" ->submit("Delete", deleteItem )
      )

      /**
       * error message if there is no Compound or Empty record
       */
    }) openOr Text("Invalid Method")
  }

}
